package com.wenchao.quickstart.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * app私有目录下保存SharedPreferences文件
 *
 * @author xuxin
 */
public class SharedUtil {

    private static String NAME = "HJLY_SP";

    public static void setName(String name) {
        NAME = name;
    }

    /**
     * 采用默认的文件名保存数据,默认以PackageName为文件名
     *
     * @param context
     * @param key
     * @param value
     */
    public static void putString(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 获取默认保存文件中的数据,默认以PackageName为文件名
     *
     * @param context
     * @param key
     * @return
     */
    public static String getString(Context context, String key) {
        if (context != null) {
            SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
            return preferences.getString(key, "");
        }
        return "";
    }

    public static void removeByKey(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.remove(key);
        editor.apply();
    }

    /**
     * 采用默认的文件名保存数据,默认以PackageName为文件名
     *
     * @param context
     * @param key
     * @param value
     */
    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 获取默认保存文件中的数据,默认以PackageName为文件名<br>
     * <p>
     * 若找不到此值默认返回 false
     *
     * @param context
     * @param key
     * @return
     */
    public static boolean getBoolean(Context context, String key) {
        if (context != null) {
            SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
            return preferences.getBoolean(key, false);
        }
        return false;
    }

    public static void putInteger(Context context, String key, int value) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static int getInteger(Context context, String key) {
        if (context != null) {
            SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
            return preferences.getInt(key, 0);
        }
        return 0;
    }

    public static void putLong(Context context, String key, long value) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public static long getLong(Context context, String key) {
        if (context != null) {
            SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
            return preferences.getLong(key, 0);
        }
        return 0;
    }

}
